package wang.zl.bookmanager1.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import wang.zl.bookmanager1.R.ResultInfo;
import wang.zl.bookmanager1.domain.Book;
import wang.zl.bookmanager1.domain.User;
import wang.zl.bookmanager1.service.impl.UserServiceImpl;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("login")
    public ResultInfo login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUsername, username);
        User fUser = userService.getOne(qw);
        if (fUser == null || !(fUser.getPassword()).equals(password)) {
            return new ResultInfo(false, null, "用户名或密码错误");
        }
        session.setAttribute("user", fUser);
        return new ResultInfo(true, fUser.getRole(), "登录成功");

    }

    @PostMapping("register")
    public ResultInfo register(User user, String checkCode, HttpSession session) {
        String o_checkCode = (String) session.getAttribute("checkCode");
        session.removeAttribute("checkCode");
        if (o_checkCode == null) {
            return new ResultInfo(false, null, "请刷新验证码");
        }

        if (checkCode == null || !o_checkCode.equalsIgnoreCase(checkCode)) {
            return new ResultInfo(false, null, "验证码错误");
        }

        String password = user.getPassword();

        user.setPassword(password);
        user.setRole(1);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User emp = userService.getOne(queryWrapper);
        if (emp == null) {
            userService.save(user);
            return new ResultInfo(true, null, "注册成功");
        } else {
            return new ResultInfo(false, null, "重复用户名，注册失败");
        }

    }

    @PostMapping("getByName")
    public ResultInfo getByName(HttpSession session) {
        String user = ((User) session.getAttribute("user")).getUsername();
        return new ResultInfo(true, user, "");
    }
    @PostMapping("getbylike")
    public ResultInfo getByLike(String text){
        if(text == null){
            text = "";
        }
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.like(User::getUsername,text).orderByAsc();
        qw.eq(User::getRole,1);
        List<User> list = userService.list(qw);
        if (list.size()!=0){
            return new ResultInfo(true,list,"成功");
        }else {
            return new ResultInfo(false,null,"暂无数据");
        }

    }
//    @PostMapping("getAllBook")
//    public ResultInfo getAl(){
//        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
//        qw.eq(User ::getRole,0);
//        userService.list(qw);
//        return new ResultInfo(true,null,"");
//    }
    @PostMapping("UpdateUser")
    public ResultInfo updateBook(User user , Long UserId){

        LambdaQueryWrapper<User> qw1 = new LambdaQueryWrapper<>();
        if(!UserId.equals(user.getId())){
            LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
            qw.eq(User::getId,user.getId());
            User one = userService.getOne(qw);

            if(one != null){
                return new ResultInfo(false,null,"编号已经存在了,请换一个");
            }else {

                qw1.eq(User::getUsername,user.getUsername());
                User one1 = userService.getOne(qw);
                if (one1 == null) {
                    userService.removeById(UserId);
                    userService.save(user);
                    return new ResultInfo(true, null, "修改成功");
                }else {
                    return new ResultInfo(false,null,"名称有相同的");
                }
            }
        }else {
            userService.removeById(UserId);

            qw1.eq(User::getUsername,user.getUsername());
            User one1 = userService.getOne(qw1);
            if (one1 == null) {
                userService.save(user);
                return new ResultInfo(true, null, "修改成功");
            }else {
                return new ResultInfo(false,null,"名称有相同的");
            }

        }
    }
}

